package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.BorderFactory;

import mbp.base.GuiFunction;
import mbp.base.ResourceType;
import mbp.core.ResourceTypeBuilder;
import mbp.core.ResourceValidation;
import mbp.exceptions.ParseExpressionErrorException;
import mbp.gui.frames.MbpManagerGui;

public class ResourceValidateGui extends JPanel implements ActionListener
{
	private MbpManagerGui mbpManagerGui;
	private JTextField txtAgentName;
	private JTextField txtCondition;
	
	private JButton btnCreate;
	private JButton btnSend;
	private JButton btnSet;
	private JButton btnCheck;
	//private JButton btnToClient;
	private JComboBox cmbValidateFc;
	private JComboBox cmbTypesXml;
	
	private ResourceValidation validateFunction;
	private HashMap<String, ResourceValidation> validateFunctionStore;
	
	private ResourceValidationEditPanel rcvEditPanel;
	
	public ResourceValidateGui(MbpManagerGui mbpManagerGui) 
	{
		this.mbpManagerGui = mbpManagerGui;
		validateFunctionStore = new HashMap<String, ResourceValidation>();
		setLayout(null);
		
		JPanel panel_0 = new JPanel();
		panel_0.setLayout(null);
		panel_0.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Resource Type Fom XML", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_0.setBounds(10, 11, 290, 70);
		add(panel_0);
		
		cmbTypesXml = new JComboBox();
		cmbTypesXml.setBounds(10, 39, 125, 20);
		panel_0.add(cmbTypesXml);
		
		btnCreate = new JButton("Create");
		btnCreate.setName("CREATE");
		btnCreate.setFont(new Font("Arial", Font.PLAIN, 11));
		btnCreate.setBounds(145, 36, 80, 23);
		panel_0.add(btnCreate);
		
		JLabel label = new JLabel("Resource type");
		label.setBounds(10, 22, 100, 14);
		panel_0.add(label);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Active validate function", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLACK));
		panel_1.setBounds(10, 92, 290, 70);
		add(panel_1);
		
		cmbValidateFc = new JComboBox();
		cmbValidateFc.setBounds(10, 39, 125, 20);
		panel_1.add(cmbValidateFc);
		
		btnCheck = new JButton("Check");
		btnCheck.setBounds(147, 36, 80, 23);
		panel_1.add(btnCheck);
		btnCheck.setName("ACC_CHECK");
		btnCheck.setFont(new Font("Arial", Font.PLAIN, 11));
		
//		btnToClient = new JButton("->");
//		btnToClient.setFont(new Font("Dialog", Font.PLAIN, 11));
//		btnToClient.setBounds(237, 36, 43, 23);
//		panel_1.add(btnToClient);
//		btnToClient.addActionListener(this);
		
		btnCheck.addActionListener(this);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Send validate function", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_2.setBounds(10, 173, 290, 70);
		add(panel_2);
		
		txtAgentName = new JTextField();
		txtAgentName.setColumns(10);
		txtAgentName.setBounds(10, 39, 125, 20);
		panel_2.add(txtAgentName);
		
		btnSend = new JButton("Send");
		btnSend.setFont(new Font("Arial", Font.PLAIN, 11));
		btnSend.setBounds(145, 38, 80, 23);
		panel_2.add(btnSend);
		
		JLabel label_1 = new JLabel("Agent name");
		label_1.setBounds(10, 24, 80, 14);
		panel_2.add(label_1);
		
		rcvEditPanel = new ResourceValidationEditPanel(GuiFunction.EDITOR, null);
	
		rcvEditPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Resource acceptability", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 70, 213)));
		rcvEditPanel.setBounds(310, 11, 290, 151);
		add(rcvEditPanel);
		
//		txtCondition = new JTextField();
//		txtCondition.setBounds(10, 39, 270, 20);
//		panel_3.add(txtCondition);
//		
//		btnSet = new JButton("Set");
//		btnSet.setName("ACC_SET");
//		btnSet.setFont(new Font("Arial", Font.PLAIN, 11));
//		btnSet.setBounds(10, 117, 80, 23);
//		panel_3.add(btnSet);
//		
//		JLabel label_2 = new JLabel("Condition");
//		label_2.setBounds(10, 25, 80, 14);
//		panel_3.add(label_2);
		btnCreate.addActionListener(this);
		btnSend.addActionListener(this);
		//btnSet.addActionListener(this);
		
		cmbValidateFc.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				int index = cmbValidateFc.getSelectedIndex();
				String key = (String)cmbValidateFc.getItemAt(index);
				if(validateFunctionStore.containsKey(key))
				{
					LoadValidateFunction(validateFunctionStore.get(key));
				}
				else
				{
					clearValidateFunction();
				}
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public ArrayList<ResourceValidation> getValidationsList()
	{
		ArrayList<ResourceValidation> validationList = new ArrayList<ResourceValidation>(validateFunctionStore.values());
		return validationList;
	}
	
	public void LoadResourceTypeList(List<String> types)
	{
		Iterator<String> it = types.iterator();
		
		while(it.hasNext())
		{
			String type = it.next();
			cmbTypesXml.addItem(type);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnCreate)
		{
			ResourceTypeBuilder typeBuilder = new ResourceTypeBuilder("");
			ResourceType tmpType = typeBuilder.GetResourceType((String)cmbTypesXml.getSelectedItem());
			ResourceValidation rvr = new ResourceValidation(tmpType);				
			AddValuesRangeFunction(rvr);
			return;
		}
		
		if(e.getSource() == btnSend)
		{
			mbpManagerGui.myAgent.SendResourceValidation(this.validateFunction, txtAgentName.getText());
			return;
		}
		
		
		if(e.getSource() == btnCheck)
		{
			boolean result = true;
			StringBuilder sb = new StringBuilder();
			try 
			{
				result = validateFunction.validateCondition(mbpManagerGui.getActiveResource());				
			} 
			catch (ParseExpressionErrorException e1) 
			{
				JOptionPane.showMessageDialog((Component) e.getSource(), e1.getStackTrace(), "InvalidResourceTypeException", JOptionPane.ERROR_MESSAGE);
				return;
			}
			catch (NullPointerException e2)
			{
				JOptionPane.showMessageDialog((Component) e.getSource(), "No active resource!!! See Resource builder tab", "NullPointerException", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			
			if(result)
			{
				JOptionPane.showMessageDialog((Component) e.getSource(), "Resource: ACCAPTABLE\n\n" + sb.toString(), "Validation result:", JOptionPane.INFORMATION_MESSAGE);
			}
			else
			{
				JOptionPane.showMessageDialog((Component) e.getSource(), "Resource: NOT ACCAPTABLE\n\n" + sb.toString(), "Validation result:", JOptionPane.ERROR_MESSAGE);
			}
			return;
		}
		
//		if(e.getSource() == btnToClient)
//		{
//			if(this.validateFunction != null)
//			{
//				mbpManagerGui.businessAgentGui.addResourceValidation(deleteActiveObject());
//			}
//		}
		
	}
	
	
	public void AddValuesRangeFunction(ResourceValidation rvr)
	{
		int items = validateFunctionStore.size();
		String name = rvr.getType().getResourceTypeName();
		name = name + " [" + items + "]";
		validateFunctionStore.put(name, rvr);
		cmbValidateFc.addItem(name);
		cmbValidateFc.setSelectedItem(name);
		
		LoadValidateFunction(rvr);
	}
	
	public void LoadValidateFunction(ResourceValidation rvr)
	{
		rcvEditPanel.LoadResourceValidation(rvr);
		this.validateFunction = rvr;
	}
	
	public void clearValidateFunction()
	{
		rcvEditPanel.clear();
		this.validateFunction = null;
	}
	
	public ResourceValidation deleteActiveObject()
	{
		int index = cmbValidateFc.getSelectedIndex();
		ResourceValidation rcv = null;
		if(index != -1)
		{
			String key = (String)cmbValidateFc.getItemAt(index);
			//rcv = validateFunctionStore.get(key);
			rcv = validateFunctionStore.remove(key);
			cmbValidateFc.removeItemAt(index);
		}		
		return rcv;
	}
}
